package br.com.j7.jdesk.ui.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;

import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;

import br.com.j7.jdesk.persistence.entity.Services;
import br.com.j7.jdesk.persistence.entity.ServicesSituations;
import br.com.j7.jdesk.persistence.entity.ServicesTypes;
import br.com.j7.jdesk.persistence.repository.ServiceRepository;
import br.com.j7.jdesk.persistence.repository.ServiceSituationRepository;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepository;
import br.com.j7.jdesk.persistence.repository.impl.ServiceRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceSituationRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceTypeRepositoryImpl;

/**
 * 
 * @author Edivando Alves
 *
 */
@ManagedBean
@ViewScoped
public class KambanBean extends WebContext implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Services service;
	
	private Map<String, Integer> mapServiceSituation;
	private Map<String, Integer> mapServiceType;
	private List<String> filterServiceSituation;
	private List<String> filterServiceType;
	
	private List<Services> newServices;
	private List<Services> developmentServices;
	private List<Services> finishedServices;
	private List<ServicesSituations> servicesSituations;
	private List<ServicesTypes> servicesTypes;
	
	private ServiceRepository serviceRepository;
	private ServiceSituationRepository serviceSituationRepository;
	private ServiceTypeRepository serviceTypeRepository;
	
	private DashboardModel model;
	
	public KambanBean() {
		serviceRepository = new ServiceRepositoryImpl();
		serviceSituationRepository = new ServiceSituationRepositoryImpl();
		serviceTypeRepository = new ServiceTypeRepositoryImpl();
		
		service = new Services();
		populeMapServiceSituation();
		populeMapServiceType();
		
		
		
		model = new DefaultDashboardModel();  
        DashboardColumn column1 = new DefaultDashboardColumn();  
        DashboardColumn column2 = new DefaultDashboardColumn();  
        DashboardColumn column3 = new DefaultDashboardColumn();  
        DashboardColumn column4 = new DefaultDashboardColumn();  
        DashboardColumn column5 = new DefaultDashboardColumn();  
        DashboardColumn column6 = new DefaultDashboardColumn();  
        DashboardColumn column7 = new DefaultDashboardColumn();  
        DashboardColumn column8 = new DefaultDashboardColumn();  
        DashboardColumn column9 = new DefaultDashboardColumn();  
          
        column1.addWidget("default");
        column2.addWidget("default");
        column3.addWidget("default");
        column4.addWidget("default");
        column5.addWidget("default");
        column6.addWidget("default");
        column7.addWidget("default");
        column8.addWidget("default");
        column9.addWidget("default");
        
        column1.addWidget("sports");  
        column1.addWidget("finance");

          
        column2.addWidget("lifestyle");  
        column2.addWidget("weather");
          
        column3.addWidget("politics");  

        
  
        model.addColumn(column1);  
        model.addColumn(column2);  
        model.addColumn(column3);  
        model.addColumn(column4);
        model.addColumn(column5);
        model.addColumn(column6);
        model.addColumn(column7);
        model.addColumn(column8);
        model.addColumn(column9);
		
	}

	public DashboardModel getModel() {
		return model;
	}
	
	private void populeMapServiceSituation() {
		mapServiceSituation = new LinkedHashMap<>();
		for (ServicesSituations ss : getServicesSituations()) {
			mapServiceSituation.put(ss.getCdServiceSituation(),ss.getIdServiceSituation());
		}	
	}
	
	private void populeMapServiceType() {
		mapServiceType = new LinkedHashMap<>();
		for (ServicesTypes st : getServicesTypes()) {
			mapServiceType.put(st.getCdServiceType(), st.getIdServiceType());
		}
	}

	public String adicionarService(){
		try {
			this.service.setEnterprise(getSessionEnterprise());
			this.service.setUser(getSessionUser());
			serviceRepository.update(this.service);
			this.service = new Services();
			this.newServices = null;
			this.developmentServices = null;
			this.finishedServices = null;
			return "kanban";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public List<Services> getNewServices() {
		try {
			if(newServices == null){
				newServices = serviceRepository.listServiceByIdStateAndEnterprise(1, getSessionEnterprise(), getFilterServiceType());
			}
			return newServices;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public List<Services> getDevelopmentServices() {
		try {
			if( developmentServices == null){
				developmentServices = serviceRepository.listServiceByIdStateAndEnterprise(2, getSessionEnterprise(),getFilterServiceType());
			}
			return developmentServices;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public List<Services> getFinishedServices() {
		try {
			if( finishedServices  == null){
				finishedServices = serviceRepository.listServiceByIdStateAndEnterprise(3, getSessionEnterprise(), filterServiceType);
			}
			return finishedServices;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public String filterKamban( ValueChangeEvent event ){
		newServices = null;
		developmentServices = null;
		finishedServices = null;
		
		
		event.getNewValue();
		System.out.println();
		return "kamban";
	}
	
	public Services getService() {
		return service;
	}
	
	public List<ServicesSituations> getServicesSituations() {
		try {
			if(servicesSituations == null){
				servicesSituations = serviceSituationRepository.listByEnterprise(getSessionEnterprise());
			}
			return servicesSituations;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<ServicesTypes> getServicesTypes() {
		try {
			if(servicesTypes == null){
				servicesTypes = serviceTypeRepository.listByEnterprise(getSessionEnterprise());
			}
			return servicesTypes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	public void setService(Services service) {
		this.service = service;
	}
	
	public void findService(){
		System.out.println(service);
	}
	
	public Map<String, Integer> getMapServiceSituation() {
		return mapServiceSituation;
	}
	
	public Map<String, Integer> getMapServiceType() {
		return mapServiceType;
	}
	
	public List<String> getFilterServiceSituation() {
		if( filterServiceSituation == null ){
			filterServiceSituation = new ArrayList<>();
		}
		return filterServiceSituation;
	}
	
	public void setFilterServiceSituation(List<String> filterServiceSituation) {
		this.filterServiceSituation = filterServiceSituation;
	}
	
	public List<String> getFilterServiceType() {
		if( filterServiceType == null ){
			filterServiceType = new ArrayList<>();
		}
		return filterServiceType;
	}
	
	public void setFilterServiceType(List<String> filterServiceType) {
		this.filterServiceType = filterServiceType;
	}

}
